package com.ewzj.bigdata.mvp.presenter

import android.app.Activity
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.utils.RxUtils
import com.ewzj.bigdata.mvp.contract.HourseListContract
import com.ewzj.bigdata.mvp.model.rspbean.BaseData
import com.ewzj.bigdata.mvp.model.rspbean.BaseListData
import com.ewzj.bigdata.mvp.model.rspbean.HomeBean
import com.ewzj.bigdata.mvp.model.rspbean.SubFilterBean
import com.ewzj.bigdata.mvp.ui.adapter.HomePageAdapter
import com.jess.arms.mvp.BasePresenter
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Consumer
import io.reactivex.functions.Function
import io.reactivex.observables.GroupedObservable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_informercial.*
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber
import org.w3c.dom.ls.LSException
import javax.inject.Inject

/**
 * author:zhangjing
 * 作用:
 * return:
 */
class HourseListPresenter @Inject constructor(val rxErrorHandler: RxErrorHandler,model:HourseListContract.Model,view:HourseListContract.View):BasePresenter<HourseListContract.Model,HourseListContract.View>(model,view){
    val moredatas= arrayListOf<SubFilterBean.DataEntity>()
    val context=mRootView as Activity
    var curpage=1
    var groupId:String?=null
    val datas:List<HomeBean> = arrayListOf()
    val adapter:HomePageAdapter by lazy { HomePageAdapter(datas) }
    var words:String?=null
    fun initadapter(){

    }
    fun subFilterlist(type: String){
        mRootView.initRecylerView(adapter)
        mModel.loadFilter(type).compose(RxUtils.applySchedulers(mRootView))
                .map { t: SubFilterBean? -> t?.data }
                .flatMap { t ->Observable.fromIterable(t)  }
                .groupBy { t: SubFilterBean.DataEntity? ->  t?.dics_type}
                .subscribe(object :Observer<GroupedObservable<String?, SubFilterBean.DataEntity>>{
                    override fun onError(e: Throwable?) {

                    }

                    override fun onComplete() {
                        mRootView.morePopView(moredatas)
                    }

                    override fun onSubscribe(d: Disposable?) {

                    }

                    override fun onNext(data: GroupedObservable<String?, SubFilterBean.DataEntity>) {
                            data.subscribe(Consumer { t: SubFilterBean.DataEntity ->
                            run {
                                if (data.key!!.equals("region")) {
                                    addChildItem(t.children)
                                    mRootView.areaPopView(t)
                                }else if(data.key!!.equals("price")){
                                    addChildItem(t.children)
                                    mRootView.pricePopView(t)
                                }else if (data.key!!.equals("household")){
                                    addChildItem(t.children)
                                    mRootView.typePopView(t)
                                }else {
                                    moredatas.add(t)
                                }
                            }
                        })
                    }

                })
    }

    fun loadHourses(groupId:String?, words: String, areaid:String,page: Int=1){
        curpage=1
        mModel.loadHouses(groupId,words,page,areaid).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object :ErrorHandleSubscriber<BaseData<BaseListData<HomeBean>>>(rxErrorHandler){
                    override fun onNext(t: BaseData<BaseListData<HomeBean>>) {
                        curpage++
                        adapter.setNewData(t.data!!.list)
                        adapter.emptyView?:mRootView.initAdapterEmptyView(adapter)
                        if (t.data!!.list.size<AppKey.PAGE_NUM){
                            adapter.disableLoadMoreIfNotFullPage()
                        }
                    }

                })
    }

    fun loadmoredata(groupId:String?,words: String,areaid:String){
        mModel.loadHouses(groupId,words,curpage,areaid).compose(RxUtils.bindToLifecycle(mRootView))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doAfterTerminate { if(adapter.isLoading){
                    adapter.loadMoreComplete()
                } }
                .subscribe(object :Observer<BaseData<BaseListData<HomeBean>>> {
                    override fun onComplete() {

                    }

                    override fun onNext(t: BaseData<BaseListData<HomeBean>>) {
                        curpage++
                        adapter.addData(t.data!!.list)
                        if(t.data!!.list.size<AppKey.PAGE_NUM){
                            adapter.loadMoreEnd()
                        }
                    }

                    override fun onSubscribe(d: Disposable?) {

                    }

                    override fun onError(e: Throwable?) {
                        adapter.loadMoreFail()
                    }


                }
                )

    }

    private fun addChildItem(childrenEntities: MutableList<SubFilterBean.DataEntity.ChildrenEntity>) {
        val childrenEntity = SubFilterBean.DataEntity.ChildrenEntity()
        childrenEntity.setIsseld(false)
        childrenEntity.name = "不限"
        childrenEntities.add(0, childrenEntity)
    }
}